package sqlanalysisgui.editor;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import sqlanalysisgui.view.model.FileAnalysisInfo;
import sqlanalysisgui.view.model.ProjectInfo;
import sqlanalysisgui.view.model.TableInfo;
import sqlanalysisgui.view.model.provider.FileAnalysisInfoContentProvider;
import sqlanalysisgui.view.model.provider.FileAnalysisInfoLabelProvider;
import sqlanalysisgui.view.model.provider.TableInfoContentProvider;

import com.cho.core.db.dao.TableDao;

public class DashboardTable extends EditorPart {
	public static final String ID = "sqlanalysisgui.editor.DashboardTable"; //$NON-NLS-1$
	private Logger logger =  Logger.getLogger(this.getClass());
	private Text text_projectName;
	private Text text_projectRegDate;
	private Table table;
	private Table table_1;
	TableViewer tableViewer_fileAnalysisInfo;
	TableViewer tableViewer_tableList;
	TableViewer tableViewer_notUseSourceTable;
	TableViewer tableViewer_databaseOnlyUseTable;
	
	private ProjectInfo proejctInfo;
	private DashboardTableInput input;
	private Table table_2;
	private Table table_3;
	
	public DashboardTable() {
	}

	/**
	 * Create contents of the editor part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		
		
		
		composite.setLayout(new GridLayout(1, false));
		{
			Group group = new Group(composite, SWT.NONE);
			group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			group.setLayout(new GridLayout(4, false));
			
			Label lblNewLabel = new Label(group, SWT.NONE);
			lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblNewLabel.setText("프로젝트");
			
			text_projectName = new Text(group, SWT.BORDER);
			text_projectName.setEditable(false);
			text_projectName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			text_projectName.setText(proejctInfo.getProjectName());
			
			
			
			Label lblNewLabel_1 = new Label(group, SWT.NONE);
			lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblNewLabel_1.setText("분석 날짜");
			
			text_projectRegDate = new Text(group, SWT.BORDER);
			text_projectRegDate.setEditable(false);
			text_projectRegDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
			text_projectRegDate.setText(proejctInfo.getProjectRegDate());
		}
		
		Group group_1 = new Group(composite, SWT.NONE);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		group_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		group_1.setText("파일 분석 정보");
		
		tableViewer_fileAnalysisInfo = new TableViewer(group_1, SWT.BORDER | SWT.FULL_SELECTION);
		table_1 = tableViewer_fileAnalysisInfo.getTable();
		table_1.setHeaderVisible(true);
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tableColumn = tableViewerColumn.getColumn();
			tableColumn.setText("파일수");
			tableColumn.setWidth(100);
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getTotalFile();
				}
			});
		}
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("라인수");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getTotalFileLine();
				}
			});
		}
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("SQL 사용 파일");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getUseSqlFile();
				}
			});
		}
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("INSERT");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getcType();
				}
			});
		}
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("SELECT");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getrType();
				}
			});
		}
		
		{		
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("UPDATE");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getuType();
				}
			});
		}
		
		{
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_fileAnalysisInfo, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText("DELETE");
			tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
				@Override
				public String getText(Object element) {
					FileAnalysisInfo p = (FileAnalysisInfo) element;
					return p.getdType();
				}
			});
		}
		
		
		Group group = new Group(composite, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		group.setLayout(new FillLayout(SWT.HORIZONTAL));
		group.setText("테이블 사용 빈도");
		
		tableViewer_tableList = new TableViewer(group, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer_tableList.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		createTableColumns(tableViewer_tableList);
		
/*		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setBounds(3, 15, 578, 99);
		composite_2.setLayout(null);*/
		
/*		Composite composite = new Composite(composite, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));*/
		
		Group group_3 = new Group(composite, SWT.NONE);
		group_3.setSize(233, 63);
		group_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		group_3.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		group_3.setText("DATABASE와 소스에서 사용하는 테이블(DB 접속정보 필요)");
		group_3.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		
		tableViewer_notUseSourceTable = new TableViewer(group_3, SWT.BORDER | SWT.FULL_SELECTION);
		table_2 = tableViewer_notUseSourceTable.getTable();
		table_2.setLinesVisible(true);
		table_2.setHeaderVisible(true);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer_notUseSourceTable, SWT.NONE);
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				TableInfo p = (TableInfo) element;
				return p.getTableName();
			}
		});
		TableColumn tblclmnNewColumn_1 = tableViewerColumn.getColumn();
		
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("TABLE");
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer_notUseSourceTable, SWT.NONE);
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				TableInfo p = (TableInfo) element;
				return p.getTableComment();
			}
		});		
		
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("한글명");

		
		Group grpDatabase = new Group(composite, SWT.NONE);
		grpDatabase.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		grpDatabase.setText("DATABASE에만 존재하는 테이블(DB 접속정보 필요)");
		grpDatabase.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		tableViewer_databaseOnlyUseTable = new TableViewer(grpDatabase, SWT.BORDER | SWT.FULL_SELECTION);
		table_3 = tableViewer_databaseOnlyUseTable.getTable();
		table_3.setHeaderVisible(true);
		table_3.setLinesVisible(true);
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer_databaseOnlyUseTable, SWT.NONE);
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				TableInfo p = (TableInfo) element;
				return p.getTableName();
			}
		});
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("TABLE");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer_databaseOnlyUseTable, SWT.NONE);
		tableViewerColumn_3.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				TableInfo p = (TableInfo) element;
				return p.getTableComment();
			}
		});				
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("한글명");

		
		
/*		
		Composite composite = new Composite(group_2, SWT.BORDER | SWT.V_SCROLL);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite.setBounds(0, 0, 64, 64);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));*/
		
		
		tableDataSetting();
		tblclmnNewColumn_1.pack();
		tblclmnNewColumn_3.pack();
		tblclmnNewColumn_2.pack();
		tblclmnNewColumn_4.pack();
	}

	private void createTableColumns(TableViewer tableViewer){
		String[] titles = {"테이블명", "CRUD", "INSERT", "SELECT", "UPDATE", "DELETE"};
		for (int i = 0; i < titles.length; i++) {
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
			TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
			tblclmnNewColumn.setWidth(100);
			tblclmnNewColumn.setText(titles[i]);
		}
	}
	
	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// Do the Save operation
	}

	@Override
	public void doSaveAs() {
		// Do the Save As operation
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		// Initialize the editor part
		
		logger.debug("site: " + site);
		logger.debug("site.getId: " + site.getId());
		logger.debug("input: " + input);
		
		if (!(input instanceof DashboardTableInput)) {
			throw new RuntimeException("Wrong input");
		}

		this.input = (DashboardTableInput) input;
		
		proejctInfo =  this.input.getProjectInfo();
		
		logger.debug("projectInfo 값 확인:" + proejctInfo.toString());
		setSite(site);
		setInput(input);
		//proejctInfo = MyModel.getInstance().getPersonById(this.input.getId());
		//setPartName("Person ID: " + proejctInfo.getId());		
		setPartName("DASH BOARD");		
	}
	
	private void tableDataSetting(){
		
		tableViewer_fileAnalysisInfo.setContentProvider(new ArrayContentProvider());
	    // Get the content for the viewer, setInput will call getElements in the
	    // contentProvider
		tableViewer_fileAnalysisInfo.setInput(TableDao.selectProjectFileAnalysisInfo(proejctInfo.getProjectName()));
	    // Make the selection available to other views
	    getSite().setSelectionProvider(tableViewer_fileAnalysisInfo);
	    // Set the sorter for the table
	    
	    tableViewer_notUseSourceTable.setContentProvider(new TableInfoContentProvider());
	    tableViewer_notUseSourceTable.setInput(TableDao.selectSourceUseAndDatabaseUseTableForGUI(proejctInfo.getProjectName()));
	    
	    tableViewer_databaseOnlyUseTable.setContentProvider(new TableInfoContentProvider());
	    tableViewer_databaseOnlyUseTable.setInput(TableDao.selectSourceUseAndDatabaseNotUseTableForGUI(proejctInfo.getProjectName()));
	    
	    
	    tableViewer_tableList.setContentProvider(new FileAnalysisInfoContentProvider());
	    tableViewer_tableList.setLabelProvider(new FileAnalysisInfoLabelProvider());
	    tableViewer_tableList.setInput(TableDao.selectProjectTableUseRatioAnalysisList(proejctInfo.getProjectName()));
	    
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
}
